<template>
  <div class="login_container" >
    <div class="login_box" @keyup.enter="submitForm('loginForm')">
<!--      头像区域-->
      <div class="avatar_box">
        <img src="../assets/girl.png" alt="">
      </div>
<!--      登录表单区域-->
      <el-form ref="loginForm" :model="ruleForm"  :rules="rules" class="from"  >
        <el-form-item prop="name">
          <el-input  prefix-icon="el-icon-user-solid" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary"  @click="submitForm('loginForm')">登录</el-button>
          <el-button type="info" @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data () {
    return {
      a: 10,
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      /* 对整个表单进行校验的方法，参数为一个回调函数。
      该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段。
      若不传入回调函数，则会返回一个 promise */
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          return false
        }
        const res = await this.$http.post('login', this.ruleForm)
        if (res.meta.status !== 200) {
          this.$msg.error(res.meta.msg)
          return false
        }
        // 登录成功token保存到sessionStorage中,并跳转到/home
        this.$msg.success(res.meta.msg)
        window.sessionStorage.setItem('token', res.data.token)
        await this.$router.push('/home')
      })
    },
    resetForm (formName) {
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs[formName].resetFields()
    }
  }
}

</script>
<style scoped>
  .login_container{
    height: 100%;
    background-color: #2b4b6b;;
  }
  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
  .avatar_box{
    width: 150px;
    height: 150px;
    overflow: hidden;
    border-radius: 75px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    border: 1px solid #eeeeee;
    box-shadow: 0 0 10px #ddd;
  }
  .avatar_box img{
    width: 100%;

  }

  .from{
    width: 100%;
    position: absolute;
    bottom: 0px;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btn{
    float: right;
    display: flex;
    justify-self: flex-end;
  }
</style>
